package com.vue.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vue.context.BaseContext;
import com.vue.dto.CourseDTO;
import com.vue.dto.CoursePageQueryDTO;
import com.vue.entity.Course;
import com.vue.mapper.CourseMapper;
import com.vue.mapper.TeacherMapper;
import com.vue.result.PageResult;
import com.vue.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 课程分页查询
     * @param coursePageQueryDTO
     * @return
     */
    public PageResult PageQuery(CoursePageQueryDTO coursePageQueryDTO) {

            PageHelper.startPage(coursePageQueryDTO.getPageNum(),coursePageQueryDTO.getPageSize());
            Page<Course> page = courseMapper.PageQuery(coursePageQueryDTO);

            long total = page.getTotal();
            List<Course> records = page.getResult();
            return new PageResult(total,records);
    }

    /**
     * 新增课程
     * @param courseDTO
     */
    public void add(CourseDTO courseDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO,course);
        Long id = BaseContext.getCurrentId();
        course.setTeacherId(id);
        courseMapper.insert(course);
    }

    /**
     * 修改课程
     * @param courseDTO
     */
    public void update(CourseDTO courseDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO,course);
        courseMapper.update(course);
    }

    /**
     * 根据id删除课程
     * @param id
     */
    public void deleteCourse(Long id) {
        courseMapper.deleteById(id);
    }
}
